/**
 * 创建UUID
 */
export function createUUID(len?: number, firstU?: boolean, radix?: number): string {
  const chars: string[] = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  const uuid: string[] = []
  radix = radix || chars.length

  if (len) {
    for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]
  } else {
    let r
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
    uuid[14] = '4'

    for (let i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | (Math.random() * 16)
        uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r]
      }
    }
  }
  if (firstU) {
    uuid.shift()
    return 'u' + uuid.join('')
  } else {
    return uuid.join('')
  }
}

/**
 * 生成随机数
 */
export function randomNum(minNum: number, maxNum: number): number {
  return parseInt(String(Math.random() * (maxNum - minNum + 1) + minNum))
}

/**
 * 异步任务处理
 */
export function asyncTasks(promise: Promise<any>): Promise<any[]> {
  return promise
    .then((data) => {
      return [null, data]
    })
    .catch((err) => {
      return [err, null]
    })
}
